using System;
using System.Diagnostics; // 用于启动外部进程
using System.IO;
using UnityEditor;
using UnityEngine;

namespace com.yoozoo.gta
{
    public class FBXTangentsCloseTool : EditorWindow
    {
        private string _inputMeshPath = "";
        private bool _data_Tangent = false;

        [MenuItem("HRP/FBXTangentsCloseTool")]
        public static void ShowWindow()
        {
            GetWindow<FBXTangentsCloseTool>("FBXTangentsCloseTool");
        }

        private void OnGUI()
        {
            GUILayout.Label("Mesh文件优化工具", EditorStyles.boldLabel);
            GUILayout.Space(8);
            _inputMeshPath = EditorGUILayout.TextField("Meshs Path:", _inputMeshPath);
            if (GUILayout.Button("选择待处理Mesh文件夹"))
            {
                _inputMeshPath = EditorUtility.OpenFolderPanel("选择文件夹", "", "");
            }
            GUILayout.Space(8);
            GUILayout.Label("勾选需要清理的数据：", EditorStyles.boldLabel);
            GUILayout.Space(4);
            _data_Tangent = EditorGUILayout.ToggleLeft("Tangent", _data_Tangent);

            if (GUILayout.Button("开始清理"))
            {
                ProcessMeshFiles();
            }
            GUILayout.Space(12);
            GUILayout.Label("需要清理UV或顶点色请点击以下按钮：",EditorStyles.miniLabel);
            GUILayout.Space(6);
            if (GUILayout.Button("启动FBX元数据处理工具"))
            {
                RunExternalExe();
            }
            GUILayout.Space(12);
        }

        private void ProcessMeshFiles()
        {
            if (string.IsNullOrEmpty(_inputMeshPath))
            {
                Debug.LogError("没有选择路径.");
                return;
            }
            if (!_data_Tangent)
            {
                Debug.LogError("没有选择需要清理的数据.");
                return;
            }

            try
            {
                string[] files = Directory.GetFiles(_inputMeshPath, "*.fbx", SearchOption.AllDirectories);
                UnityEngine.Debug.LogError(files.Length);
                foreach (var file in files)
                {
                    string unityFilePath = "Assets" + file.Replace(Application.dataPath, "").Replace("\\", "/");
                    UnityEngine.Debug.LogError(unityFilePath);
                    ModelImporter importer = AssetImporter.GetAtPath(unityFilePath) as ModelImporter;
                    if (importer != null)
                    {
                        if (_data_Tangent)
                        {
                            importer.importTangents = ModelImporterTangents.None;
                        }
                        
                        UnityEngine.Debug.LogWarning($"Mesh文件处理完成：{importer.name}");
                    }
                    importer.SaveAndReimport();
                }
                
                AssetDatabase.SaveAssets();
                UnityEngine.Debug.LogWarning("指定目录Mesh文件处理完成.");
                
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError($"处理文件时发生错误：{e.Message}");
            }
        }

        private void RunExternalExe()
        {
            try
            {
                // 获取当前脚本的目录
                string scriptDirectory = Path.GetDirectoryName(Application.dataPath);
                // 拼接要运行的.exe文件的路径
                string exePath = Path.Combine(scriptDirectory, "Tools/FBXProcessTool.exe");

                // 检查文件是否存在
                if (File.Exists(exePath))
                {
                    // 启动.exe文件
                    Process.Start(exePath);
                    UnityEngine.Debug.Log($"成功启动：{exePath}");
                }
                else
                {
                    UnityEngine.Debug.LogError($"找不到指定的.exe文件：{exePath}");
                }
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError($"启动.exe文件时发生错误：{e.Message}");
            }
        }
    }
}
